﻿using System;
using System.Windows;
using Automator.Classes;
using Microsoft.Win32;

namespace Automator.Windows
{
    public partial class SaveWindow : Window
    {
        private DocumentTypes _docType;
        private string _name;
        
        public SaveWindow(DocumentTypes docType, string entityName)
        {
            InitializeComponent();
            
            this._docType = docType;
            this._name = entityName;
            this.edtDatabaseName.Text = entityName;
            this.Title = String.Format("Save {0}", _docType.ToString());
        }
        
        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            switch (this._docType)
            {
                case DocumentTypes.Workflow:
                    dialog.DefaultExt = ".xml";
                    dialog.Filter = "Automator Workflow Files (*.xml)|*.xml";
                    break;
                case DocumentTypes.Solution:
                    dialog.DefaultExt = ".autosln";
                    dialog.Filter = "Automator Solution Files (*.autosln)|*.autosln";
                    break;
            }
            
            dialog.Title = String.Format("Save {0}", _docType.ToString());
            dialog.FileName = _name;
            dialog.OverwritePrompt = true;
            dialog.ValidateNames = true;
            if (dialog.ShowDialog(this).Value)
            {
                if (!rbFileSystem.IsChecked.Value)
                {
                    rbFileSystem.IsChecked = true;
                    RbFileSystem_Click(null, new RoutedEventArgs());
                }
                
                edtFileName.Text = dialog.FileName;
                if (!rbDatabase.IsEnabled)
                    BtnOk_Click(null, new RoutedEventArgs());
            }
            else
                if (!rbDatabase.IsEnabled)
                {
                    this.DialogResult = false;
                    Close();
                }
        }
        
        private void RbFileSystem_Click(object sender, RoutedEventArgs e)
        {
            edtFileName.IsEnabled = rbFileSystem.IsChecked.Value;
            edtDatabaseName.IsEnabled = rbDatabase.IsChecked.Value;
        }
        
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (Automator.Runtime.Settings.SqlConnectionString == null)
                rbDatabase.IsEnabled = false;
            else
                rbDatabase.IsChecked = true;
            RbFileSystem_Click(null, new RoutedEventArgs());
            if (!rbDatabase.IsEnabled)
                BtnSave_Click(null, new RoutedEventArgs());
        }
        
        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            if ((rbFileSystem.IsChecked.Value && !String.IsNullOrEmpty(edtFileName.Text)) ||
                (rbDatabase.IsChecked.Value && !String.IsNullOrEmpty(edtDatabaseName.Text)))
            {
                this.DialogResult = true;
                Close();
            }
        }
    }
}